﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCWeb.Models; // this is required...

namespace MVCWeb.Controllers
{
    public class OrderController : Controller
    {
        static CartInfo cartInfo = new CartInfo();
        //
        // GET: /Order/


        public ActionResult Index(int custid)
        {
            List<OrderModel> list = OrderClientServices.GetCustomerOrders(custid);
            //CartInfo cartInfo = new CartInfo();
            cartInfo.NumItemsInCart = 0;
            cartInfo.TotalPriceOfUnplacedOrders = 0.00;
            cartInfo.NumItemsInCart = cartInfo.UpdateCartInfo(list);
            ViewData["cartInfo"] = "You have " + cartInfo.NumItemsInCart + " items in your carts. " ;
            ViewData["cartInfo"] += "Your total is currently $" + cartInfo.TotalPriceOfUnplacedOrders + ".";


            return View("Index", list);
        }


        public ActionResult WebIndex(int custid)
        {
            List<OrderModel> list = OrderClientServices.GetCustomerOrders(custid);
            //CartInfo cartInfo = new CartInfo();
            cartInfo.NumItemsInCart = 0;
            cartInfo.TotalPriceOfUnplacedOrders = 0.00;
            cartInfo.NumItemsInCart = cartInfo.UpdateCartInfo(list);
            ViewData["cartInfo"] = "You have " + cartInfo.NumItemsInCart + " items in your carts. ";
            ViewData["cartInfo"] += "Your total is currently $" + cartInfo.TotalPriceOfUnplacedOrders + ".";


            return View("WebIndex", list);
        } 


        public ActionResult Details(int id)
        {
   

            List<OrderItemModel> list = new List<OrderItemModel>();
            list = OrderItemClientService.GetOrderProducts(id);

            ViewData["OrderTotal"] = OrderClientServices.GetTotalPrice(id);

            ViewData["cartInfo"] = "You have " + cartInfo.NumItemsInCart + " items in your carts. ";
            ViewData["cartInfo"] += "Your total is currently $" + cartInfo.TotalPriceOfUnplacedOrders + ".";
            return View("Details", list);

        }

        public ActionResult WebDetails(int id)
        {
       

            List<OrderItemModel> list = new List<OrderItemModel>();
            list = OrderItemClientService.GetOrderProducts(id);

            ViewData["OrderTotal"] = OrderClientServices.GetTotalPrice(id);

            ViewData["cartInfo"] = "You have " + cartInfo.NumItemsInCart + " items in your carts. ";
            ViewData["cartInfo"] += "Your total is currently $" + cartInfo.TotalPriceOfUnplacedOrders + ".";
            return View("WebDetails", list);

        }

        //
        // GET: /Order/Details/5

        public String TotalPrice(int id)
        {
            UrlHelper url = new UrlHelper(HttpContext.Request.RequestContext);
            ViewData["breadCrumbData"] = "<a href='" + url.Action("Index", "Order") + "'>Order Details</a>";
            ViewData["breadCrumbData"] += " > Total Price";

            double price = OrderClientServices.GetTotalPrice(id);
            string ret = price.ToString();

            return ret;
        }

        //
        // GET: /Order/Details/5
        public ActionResult CheckOrderStatus(int id)
        {

            int orderplaced = OrderClientServices.SeeIfOrderPlaced(id);

            return View("OrderStatus", orderplaced);
        }


     //   [HttpPost]
        public ActionResult Create(int custid)
        {
            try
            {
                OrderModel order = new OrderModel();
                order.customer_id = custid;

                OrderClientServices.CreateOrder(order);

                return RedirectToAction("Index");
              //  return View("Index");
            }
            catch
            {
                return View();
            }
        }


        //   [HttpPost]
        public ActionResult WebCreate(int custid)
        {
            try
            {
                OrderModel order = new OrderModel();
                order.customer_id = custid;

                OrderClientServices.CreateOrder(order);

                return RedirectToAction("WebIndex");
                //  return View("Index");
            }
            catch
            {
                return View();
            }
        }

        
        public ActionResult PlaceOrder(int placeid)
        {
                List<OrderItemModel> list = OrderItemClientService.GetOrderProducts(placeid);
                ViewData["cartInfo"] = "You have " + cartInfo.NumItemsInCart + " items in your carts. ";
                ViewData["cartInfo"] += "Your total is currently $" + cartInfo.TotalPriceOfUnplacedOrders + ".";

                if (list.Count == 0)
                {
                    return View("NoOrderItems");
                }
                else
                {
                    OrderClientServices.PlaceOrder(placeid);
                    return View("PlaceOrder", list);
                }
        }

        public ActionResult WebPlaceOrder(int placeid)
        {
            List<OrderItemModel> list = OrderItemClientService.GetOrderProducts(placeid);
            ViewData["cartInfo"] = "You have " + cartInfo.NumItemsInCart + " items in your carts. ";
            ViewData["cartInfo"] += "Your total is currently $" + cartInfo.TotalPriceOfUnplacedOrders + ".";

            if (list.Count == 0)
            {
                return View("NoOrderItems");
            }
            else
            {
                OrderClientServices.PlaceOrder(placeid);
                return View("WebPlaceOrder", list);
            }
        }

        public ActionResult CreateItem(int pid)
        {
  
            OrderItemModel order = new OrderItemModel();
            order.product_id = pid;
            return View("CreateItem", order);
        }

        [HttpPost]
        public ActionResult CreateItem(FormCollection collection)
        {
            try
            {
                OrderItemModel item = new OrderItemModel();
                item.product_id = Convert.ToInt32(collection["product_id"]);
                item.order_id = Convert.ToInt32(collection["order_id"]);
                item.quantity = Convert.ToInt32(collection["quantity"]);

                OrderItemClientService.InsertOrderItems(item);
                return RedirectToAction("Index");

            }
            catch
            {

                return View();
            }
        }



        public ActionResult WebCreateItem(int pid)
        {
            UrlHelper url = new UrlHelper(HttpContext.Request.RequestContext);
            OrderItemModel order = new OrderItemModel();
            order.product_id = pid;
            return View("WebCreateItem", order);
        }

        [HttpPost]
        public ActionResult WebCreateItem(FormCollection collection)
        {
            try
            {
                OrderItemModel item = new OrderItemModel();
                item.product_id = Convert.ToInt32(collection["product_id"]);
                item.order_id = Convert.ToInt32(collection["order_id"]);
                item.quantity = Convert.ToInt32(collection["quantity"]);

                OrderItemClientService.InsertOrderItems(item);
                return RedirectToAction("WebIndex");

            }
            catch
            {

                return View();
            }
        }

        //
        // GET: Delete Order product from cart

        public ActionResult Delete(int oid, int pid)
        {
                OrderItemClientService.DeleteOrderItems(oid, pid);
                return RedirectToAction("Index");
        }

        public ActionResult WebDelete(int oid, int pid)
        {
            OrderItemClientService.DeleteOrderItems(oid, pid);
            return RedirectToAction("WebIndex");
        }

    }
}
